package com.foo.common.base.utils;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.web.context.ContextLoader;

import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FooUtils {

	public final static int ddl_pk_length = 36;// uuid2模式下，36是标准的id长度
	public final static String session_user_id = "uid";

	/**
	 * 该方法的意义在于返回一个自定义的消息。即ret=-1
	 * 
	 * @param response
	 */
	public static void printJsonFromMap(HttpServletResponse response,
			Map<String, Object> myResultMap) {
		try {
			Gson gson = new GsonBuilder().disableHtmlEscaping()
					.serializeNulls().create();
			response.setCharacterEncoding(Charsets.UTF_8.toString());
			response.setContentType("text/plain");
			response.getWriter().write(gson.toJson(myResultMap));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 该方法的意义在于返回一个成功的消息。即ret=0
	 * 
	 * @param response
	 */
	public static void printSuccessJson(HttpServletResponse response) {
		Map<String, Object> myResultMap = Maps.newHashMap();
		myResultMap.put("ret", 0);
		myResultMap.put("msg", "");
		printJsonFromMap(response, myResultMap);
	}

	/**
	 * 该方法的意义在于返回一个成功的消息。即ret=-1
	 * 
	 * @param response
	 */
	public static void printFailJson(HttpServletResponse response, String msg) {
		Map<String, Object> myResultMap = Maps.newHashMap();
		myResultMap.put("ret", -1);
		myResultMap.put("msg", msg);
		printJsonFromMap(response, myResultMap);
	}

	/**
	 * @param response
	 * @param myObject
	 *            需要传递到前台的json对象应该封装到msg里面
	 */
	public static void printSuccessJson(HttpServletResponse response,
			Object myObject) {
		Map<String, Object> myResultMap = Maps.newHashMap();
		myResultMap.put("ret", 0);
		myResultMap.put("msg", myObject);
		printJsonFromMap(response, myResultMap);
	}

	/**
	 * 获得Spring注册的Bean的快捷方法
	 * 
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	public static <T> T getBean(Class<T> clazz) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(clazz);
	}

	/**
	 * 转化为年到秒的时间格式
	 * 
	 * @param dateTime
	 * @return
	 */
	public static String toDateFromYear2Second(DateTime dateTime) {
		return dateTime.toString("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 转化为年到秒的时间格式
	 * 
	 * @param date
	 * @return
	 */
	public static Date toDateFromYear2Second(String date) {
		return DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
				.parseDateTime(date).toDate();
	}

	/**
	 * 转化为年到天的时间格式
	 * 
	 * @param dateTime
	 * @return
	 */
	public static String toDateFromYear2Day(DateTime dateTime) {
		return dateTime.toString("yyyy-MM-dd");
	}

	/**
	 * 转化为年到天的时间格式
	 * 
	 * @param date
	 * @return
	 */
	public static Date toDateFromYear2Day(String date) {
		return DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(date)
				.toDate();
	}

	/**
	 * 转化为年到分钟的时间格式
	 * 
	 * @param dateTime
	 * @return
	 */
	public static String toDateFromYear2Minute(DateTime dateTime) {
		return dateTime.toString("yyyy-MM-dd HH:mm");
	}

	/**
	 * 转化为年到分钟的时间格式
	 * 
	 * @param date
	 * @return
	 */
	public static Date toDateFromYear2Minute(String date) {
		return DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
				.parseDateTime(date).toDate();
	}

	public static String formatDouble(String result) {
		return String.format("%1$.2f", Double.parseDouble(result));
	}

	public static String formatDouble(double result) {
		return String.format("%1$.2f", result);
	}

	public static String generateUUID() {
		return UUID.randomUUID().toString();
	}

}
